home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / TABCTRL.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  18KB  |  556 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.12  $
  6. //
  7. // Definition of class TTabItem and TTabControl.
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_TABCTRL_H)
  10. #define OWL_TABCTRL_H
  11.  
  12. #if !defined(OWL_COMMCTRL_H)
  13. # include <owl/commctrl.h>
  14. #endif
  15. #if !defined(OWL_CONTAIN_H)
  16. # include <owl/contain.h>
  17. #endif
  18. #if !defined(OWL_UPDOWN_H)
  19. # include <owl/updown.h>
  20. #endif
  21.  
  22. #if defined(BI_NAMESPACE)
  23. namespace OWL {
  24. #endif
  25.  
  26. // Mentioned in documentation but currently missing from system headers
  27. //
  28. #if !defined(TCIF_ALL)
  29. # define TCIF_ALL  (TCIF_TEXT|TCIF_IMAGE|TCIF_PARAM)
  30. #endif
  31.  
  32. // Forward ref.
  33. //
  34. class _OWLCLASS TFont;
  35. class _OWLCLASS TTabControl;
  36. class _OWLCLASS TTabEntryInternal;
  37.  
  38. // Generic definitions/compiler options (eg. alignment) preceeding the 
  39. // definition of classes
  40. #include <services/preclass.h>
  41.  
  42. //
  43. // class TTabItem
  44. // ~~~~~ ~~~~~~~~
  45. //  TTabItem encapsulates the attributes of a single tab within a 
  46. //  tab control. For example, it holds the string containing the
  47. //  tab's text.
  48. //
  49. class _OWLCLASS TTabItem : public TC_ITEM {
  50.   public:
  51.     TTabItem(uint mask);
  52.     TTabItem(const TC_ITEM& tbItem);
  53.     TTabItem(const TTabControl& ctl, int index, uint mask,
  54.              int buffLen = 0, char far* buffer = 0);
  55.  
  56.     // Used for setting label
  57.     //
  58.     TTabItem(const char far* str, int buffLen = 0, TParam2 param = 0);
  59.  
  60.     // Used for setting image
  61.     //
  62.     TTabItem(int imageIndex, TParam2 param);
  63.  
  64.     // Used for setting both image and label
  65.     //
  66.     TTabItem(int imageIndex, const char far*str);
  67.  
  68.     void    SetLabel(const char far* str, int len = 0);
  69.     void    SetIcon(int imageIndex);
  70.     void    SetParam(TParam2 lp);
  71. };
  72.  
  73.  
  74. #if !defined(OWL_NATIVECTRL_ALWAYS)
  75. //
  76. // Class TTabEntryInternal
  77. // ~~~~~ ~~~~~~~~~~~~~~~~~
  78. // TTabEntryInternal is a helper class used by TTabControl when the latter
  79. // implements Tabs without the help of the Common Control Library..
  80. //
  81. // NOTE: You should not rely on the implementation of this class. It's
  82. //       for internal use only.
  83. //
  84. class _OWLCLASS TTabEntryInternal : public TTabItem {
  85.   public:
  86.     TTabEntryInternal(const TC_ITEM& tbItem);
  87.    ~TTabEntryInternal();
  88.  
  89.     TRect   Rect;           // Actual Location of item in Window
  90.     TSize   Size;           // Desired Size of Tab
  91.     int     Row;            // Row number (MultiLine only)
  92.  
  93.   private:
  94.     // Hidden to prevent accidental copying or assignment
  95.     //
  96.     TTabEntryInternal(const TTabEntryInternal&);
  97.     TTabEntryInternal& operator =(const TTabEntryInternal&);
  98. };
  99. #endif  //  !OWL_NATIVECTRL_ALWAYS
  100.  
  101. //
  102. // class TTabHitTestInfo
  103. // ~~~~~ ~~~~~~~~~~~~~~~
  104. //  TTabHitTestInfo is a thin [very thin] wrapper around
  105. //  the TC_HITTESTINFO structure. It's a place-holder for
  106. //  future ObjectWindows enhancements for tabcontrol hit
  107. //  testing..
  108. //
  109. class _OWLCLASS TTabHitTestInfo : public TC_HITTESTINFO {
  110. };
  111.  
  112. //
  113. // class TTabControl
  114. // ~~~~~ ~~~~~~~~~~~
  115. //  TTabControl encapsulates the tab control - a window that provides
  116. //  a user interface analogous to dividers in a notebook.
  117. //
  118. class _OWLCLASS TTabControl : public TControl {
  119.   public:
  120.     TTabControl(TWindow* parent,
  121.                 int             id,
  122.                 int x, int y, int w, int h,
  123.                 TModule*        module = 0);
  124.  
  125.     TTabControl(TWindow* parent, int resourceId, TModule* module = 0);
  126.     TTabControl(HWND hwnd);
  127.    ~TTabControl();
  128.  
  129.     // Add/remove tab items
  130.     //
  131.     int         Add(const TTabItem&);
  132.     int         Add(const char far* tabText);
  133.     int         Insert(const TTabItem&, int index);
  134.     int         Insert(const char far* tabText, int index);
  135.     bool        Delete(int index);
  136.     bool        DeleteAll();
  137.  
  138.     // Set/Querry attributes of TabControl
  139.     //
  140.     int         GetCount() const;
  141.     int         GetRowCount() const;
  142.     int         GetSel() const;
  143.     int         SetSel(int index);
  144.  
  145.     // Set/Querry attributes of Tab Items
  146.     //
  147.     bool        GetItem(int index, TTabItem& item) const;
  148.     bool        GetItemRect(int index, TRect& rect) const;
  149.     bool        SetItem(int index, const TTabItem& item);
  150.     bool        SetItemExtra(int extra);
  151.     TSize       SetItemSize(const TSize& size);
  152.     void        SetPadding(const TSize& size);
  153.  
  154.     // Set/Querry attributes of control window
  155.     //
  156.     void        AdjustRect(bool clientInWindowOut, TRect& rect);
  157.     HIMAGELIST  GetImageList() const;
  158.     HIMAGELIST  SetImageList(HIMAGELIST);
  159.     void        RemoveImage(int index);
  160.  
  161.     // Tooltip
  162.     //
  163.     HWND        GetToolTips() const;
  164.     void        SetToolTips(HWND toolTip);
  165.  
  166.     int         HitTest(TTabHitTestInfo&);
  167.  
  168.     // Override TWindow virtual member function to handle transfers
  169.     //
  170.     uint        Transfer(void* buffer, TTransferDirection direction);
  171.  
  172.   protected:
  173.  
  174.     // Override TWindow virtual member functions
  175.     //
  176.     char far*   GetClassName();
  177.  
  178.     // Override TWindow handlers
  179.     //
  180.     void        EvHScroll(uint scrollCode, uint thumbPos, THandle hWndCtl);
  181.     void        EvVScroll(uint scrollCode, uint thumbPos, THandle hWndCtl);
  182.  
  183.   private:
  184.     // Hidden to prevent accidental copying or assignment
  185.     //
  186.     TTabControl(const TTabControl&);
  187.     TTabControl& operator =(const TTabControl&);
  188.  
  189. #if !defined(OWL_NATIVECTRL_ALWAYS)
  190.  
  191.     // The following data members and methods are used when the TTabControl
  192.     // object provides the underlying implementation of 'TabControls'.
  193.     // They are private to this class.
  194.     //
  195.     void        InitCtl();
  196.     void        CleanupCtl();
  197.     void        SetupFont(HFONT font = 0);
  198.     void        SetupNormalFont(TFont* normalFont);
  199.     void        SetupSelectFont(TFont* selectFont);
  200.  
  201.     void        GetWindowClass(WNDCLASS& wndClass);
  202.     void        SetupWindow();
  203.  
  204.     // Message Handlers
  205.     //
  206.     void        EvSize(uint sizeType, TSize& size);
  207.     void        EvLButtonDown(uint modKeys, TPoint& point);
  208.     void        EvSetFocus(THandle hWndLostFocus);
  209.     void        EvKillFocus(THandle hWndGetFocus);
  210.     uint        EvGetDlgCode(MSG far* msg);
  211.     void        EvKeyDown(uint key, uint repeatCount, uint flags);
  212.  
  213.  
  214.     // NOTE: The Paint() method will only be called if this
  215.     //       object does not encapsulate a predefined class.
  216.     //       See TControl::EvPaint()
  217.     //
  218.     void        Paint(TDC& dc, bool erase, TRect& rect);
  219.     void        PaintTab(TDC& dc, const TRect& tbRect, const char far* txt,
  220.                          bool clip = false);
  221.     void        PaintTabBorder(TDC& dc, const TRect& tbClient,
  222.                                const TRect* selTab = 0);
  223.     void        DrawFocusRect(TDC& dc, bool inFocus);
  224.     void        InvalidateTabRect(int index);
  225.  
  226.  
  227.     void        SetTabRect(int index, TRect* newRect);
  228.     void        SetTabRects(int firstTab = 0);
  229.     void        SetTabRectsSingle(int firstTab);
  230.     void        SetTabSize(int index, int width = 0, int height = 0);
  231.     int         TabFromPoint(const TPoint& pt) const;
  232.  
  233.     int         GetFirstVisibleTab() const;
  234.     void        SetFirstVisibleTab(int);
  235.     void        NotifyAndSelect(int index, bool bringInView = false);
  236.     void        UpdateScrollRange();
  237.  
  238.     // Querry region of various components currently laid out in the control
  239.     //
  240.     void        GetTabsArea(TRect& rect);
  241.     void        GetClientArea(TRect& rect, bool innerRect = false);
  242.     void        GetSelTabArea(TRect& rect);
  243.     void        GetScrollerArea(TRect& rect);
  244.  
  245.     // Querry whether a tab is visible or clipped
  246.     //
  247.     bool        IsVisible(int index);
  248.     bool        IsClipped(int index);
  249.  
  250.     TUpDown*    Updown;               // Points to scroller (single row only)
  251.     TFont*      NormalFont;           // Points to font of non-selected tabs
  252.     TFont*      SelectFont;           // Points to font of selected tabs
  253.     int         SelectedTab;          // Index of selected tab
  254.     int         FirstVisibleTab;      // Index of upper left tab
  255.     int         NumRows;              // Num of rows (Currently still single)
  256.     TSize       Padding;              // Padding between tabs
  257.     TSize       TabSize;              // Desired height (and width) of tabs
  258.     TCollection<TTabEntryInternal>* TabList;
  259.  
  260. #endif  //  !OWL_NATIVECTRL_ALWAYS
  261.  
  262.   DECLARE_RESPONSE_TABLE(TTabControl);
  263. };
  264.  
  265. // Generic definitions/compiler options (eg. alignment) following the 
  266. // definition of classes
  267. #include <services/posclass.h>
  268.  
  269. #if defined(BI_NAMESPACE)
  270. } // namespace OWL
  271. #endif
  272.  
  273. //----------------------------------------------------------------------------
  274. // Inlines implementations
  275. //
  276.  
  277. //
  278. // Constructor for a Tab Item:
  279. // This constructor is useful when creating a TabItem (TC_ITEM) structure
  280. // which will be filled with information about an existing tab in a
  281. // tab control. For example,
  282. //
  283. //              TTabItem item(TCIF_IMAGE|TCIF_PARAM);
  284. //              tabCtrl.GetItem(index, item);
  285. //
  286. inline TTabItem::TTabItem(uint msk) {
  287.   mask = msk;
  288. }
  289.  
  290. //
  291. // Constructs a 'TTabItem' object from a 'TC_ITEM' structure using the
  292. // assignment operator.
  293. // NOTE:  Default assignment operator is fine even if we get a shallow copy
  294. //        for 'pszText' since the effective lifetime of a 'TTabItem' is
  295. //        rather short and the underlying control copies/caches the item's
  296. //        label
  297. //
  298. inline TTabItem::TTabItem(const TC_ITEM& tbItem)
  299. {
  300.   *((TC_ITEM*)this) = tbItem;
  301. }
  302.  
  303. //
  304. // Sets the index of the image assiciated with the tab represented
  305. // by this item structure.
  306. //
  307. inline void TTabItem::SetIcon(int imageIndex)
  308. {
  309.   iImage = imageIndex;
  310.   mask |= TCIF_IMAGE;
  311. }
  312.  
  313. //
  314. // Sets the user-defined data associated with the tab represented
  315. // by this item structure.
  316. //
  317. inline void TTabItem::SetParam(TParam2 param)
  318. {
  319.   lParam = param;
  320.   mask |= TCIF_PARAM;
  321. }
  322.  
  323. #if !defined(OWL_NATIVECTRL_ALWAYS)
  324. //
  325. // Destructor of TTabEntryInternal
  326. //
  327. // Cleans up memory allocated for copy of Label
  328. //
  329. inline
  330. TTabEntryInternal::~TTabEntryInternal()
  331. {
  332.   delete[] pszText;
  333. }
  334. #endif
  335.  
  336. //----------------------------------------------------------------------------
  337. // When ObjectWindows uses the native implementation of TabControl, the
  338. // methods to manipulate the object are simple wrappers around predefined
  339. // Tab Control messages - i.e. TCM_xxxx
  340. //
  341. #if defined(OWL_NATIVECTRL_ALWAYS)
  342.  
  343. // Adds a new tab described by the 'item' parameter to the tab control.
  344. // The return value is the index of the new tab or -1 in case of error.
  345. //
  346. inline int TTabControl::Add(const TTabItem& item)
  347. {
  348.   return Insert(item, GetCount());
  349. }
  350.  
  351. // Inserts a new tab described by the 'item' parameter to the tab
  352. // control at the position specified by the 'index' parameter.
  353. // The return value is the index of the new tab or -1 in case of error.
  354. //
  355. inline int TTabControl::Insert(const TTabItem& item, int index)
  356. {
  357.   return (int)SendMessage(TCM_INSERTITEM, index,
  358.                             TParam2((const TC_ITEM far*)(&item)));
  359. }
  360.  
  361. //
  362. // Removes the item at the position specified by the 'index' parameter.
  363. // Returns 'true' if successful or 'false' otherwise.
  364. //
  365. inline bool TTabControl::Delete(int index)
  366. {
  367.   return SendMessage(TCM_DELETEITEM, index) != 0;
  368. }
  369.  
  370. //
  371. // Removes all items from the tab control. Returns 'true' if successful or
  372. // 'false' otherwise.
  373. //
  374. inline bool TTabControl::DeleteAll()
  375. {
  376.   return SendMessage(TCM_DELETEALLITEMS) != 0;
  377. }
  378.  
  379. //
  380. // Returns the number of tab items in the tab control.
  381. //
  382. inline int TTabControl::GetCount() const
  383. {
  384.   return (int)CONST_CAST(TTabControl*, this)->SendMessage(TCM_GETITEMCOUNT);
  385. }
  386.  
  387. //
  388. // Retrieves the current number of rows in the tab control.
  389. // NOTE: Only tabs with the TCS_MULTILINE style can have multiple rows.
  390. //
  391. inline int TTabControl::GetRowCount() const
  392. {
  393.   return (int)CONST_CAST(TTabControl*, this)->SendMessage(TCM_GETROWCOUNT);
  394. }
  395.  
  396. //
  397. // Returns the index of the currently selected tab item in the tab control.
  398. // Returns -1 if no tab is selected.
  399. //
  400. inline int TTabControl::GetSel() const
  401. {
  402.   return (int)CONST_CAST(TTabControl*, this)->SendMessage(TCM_GETCURSEL);
  403. }
  404.  
  405. //
  406. // Selects the tab item at the position specified by the 'index' parameter.
  407. // The return value is the index of the previously selected tab item if
  408. // successful or -1 otherwise.
  409. // NOTE: A tab control does not send TCN_SELCHANGING or TCN_SELCHANGE 
  410. //       notifications when a tab item is selected via this method.
  411. //
  412. inline int TTabControl::SetSel(int index)
  413. {
  414.   return (int)SendMessage(TCM_SETCURSEL, index);
  415. }
  416.  
  417. //
  418. // Retrieves information about the tab at the position specified by
  419. // the 'index' parameter. Returns true if successful or false otherwise.
  420. // NOTE: The 'mask' member of the 'item' structure specifies which
  421. //       attributes of the tab to return. When spefying TCIF_TEXT, item's
  422. //       pszText member must point to a valid buffer and cchTextMax must
  423. //       specify the size of the buffer.
  424. //
  425. inline bool TTabControl::GetItem(int index, TTabItem& item) const
  426. {
  427.   PRECONDITION(!(item.mask & TCIF_TEXT) || item.pszText);
  428.   return CONST_CAST(TTabControl*, this)->SendMessage(TCM_GETITEM, index,
  429.                                          TParam2((TC_ITEM far*)&item)) != 0;
  430. }
  431.  
  432. //
  433. // Retrieves the bounding rectangle of a tab within a tab control.
  434. // Returns true if successful or false otherwise. 
  435. // NOTE: 'rect' receives the position in viewport coordinates.
  436. //
  437. inline bool TTabControl::GetItemRect(int index, TRect& rect) const
  438. {
  439.   return CONST_CAST(TTabControl*, this)->SendMessage(TCM_GETITEMRECT,
  440.                                        index, TParam2((LPRECT)&rect)) != 0;
  441. }
  442.  
  443. //
  444. // Sets some or all of a tab's attributes. The 'mask' member of the
  445. // 'item' parameter specifies which attributes to set.
  446. // Returns true if successful or false otherwise.
  447. //
  448. inline bool TTabControl::SetItem(int index, const TTabItem& item)
  449. {
  450.   return SendMessage(TCM_SETITEM, index,
  451.                        TParam2((const TC_ITEM far*)&item)) != 0;
  452. }
  453.  
  454. //
  455. // Sets the number of bytes per tab reserved for application-defined
  456. // data in a tab control. Returns true if successful or false otherwise.
  457. // NOTE: This method should be invoked only when the tab control does not
  458. //       contain any tabs.
  459. //
  460. inline bool TTabControl::SetItemExtra(int extra)
  461. {
  462.   return SendMessage(TCM_SETITEMEXTRA, extra) != 0;
  463. }
  464.  
  465. //
  466. // Sets the size (width/height) of tabs in a fixed-width or owner-draw
  467. // tab control. Returns a TSize object containing the old width and height.
  468. //
  469. inline TSize TTabControl::SetItemSize(const TSize& size)
  470. {
  471.   return TSize(SendMessage(TCM_SETITEMSIZE, 0, MkParam2(size.cx, size.cy)));
  472. }
  473.  
  474. //
  475. // Sets the amount of space around each tab's icon and label in a tab 
  476. // control. 
  477. //
  478. inline void TTabControl::SetPadding(const TSize& size)
  479. {
  480.   SendMessage(TCM_SETPADDING, 0, MkParam2(size.cx, size.cy));
  481. }
  482.  
  483. //
  484. // If the 'clientInWindowOut' parameter is false, this method calculates
  485. // the display area of a tab control's display from a window rectangle
  486. // specified by the 'rect' parameter. Otherwise, the method calculates
  487. // the window rectangle that would correspond to display area specified
  488. // by the 'rect' parameter. The 'rect' parameter receives the newly computed
  489. // rectangle.
  490. //
  491. inline void TTabControl::AdjustRect(bool clientInWindowOut, TRect& rect)
  492. {
  493.   SendMessage(TCM_ADJUSTRECT, TParam1(clientInWindowOut), TParam2(&rect));
  494. }
  495.  
  496. //
  497. // Retrieves the ImageList associated with a tab control. Returns 0 if
  498. // unsuccessful.
  499. //
  500. inline HIMAGELIST TTabControl::GetImageList() const
  501. {
  502.   return (HIMAGELIST)
  503.           CONST_CAST(TTabControl*, this)->SendMessage(TCM_GETIMAGELIST);
  504. }
  505.  
  506. //
  507. // Assigns an imagelist to the tab control. Returns the handle of the
  508. // previous imagelist or 0 if there is no previous image list.
  509. //
  510. inline HIMAGELIST TTabControl::SetImageList(HIMAGELIST himl)
  511. {
  512.   return (HIMAGELIST)SendMessage(TCM_SETIMAGELIST, 0, TParam2(himl));
  513. }
  514.  
  515. //
  516. // Removes the image at the position specified by 'index' from the
  517. // imagelist associated with the tab control.
  518. // NOTE: The tab automatically updates each tab's image index so each
  519. //       tab remains associated with the same image it had been.
  520. //
  521. inline void TTabControl::RemoveImage(int index)
  522. {
  523.   SendMessage(TCM_REMOVEIMAGE, index);
  524. }
  525.  
  526. //
  527. // Retrieves the handle of the tooltip control associated with the 
  528. // tab control. Returns 0 if unsuccessful.
  529. //
  530. inline TWindow::THandle TTabControl::GetToolTips() const
  531. {
  532.   return THandle(CONST_CAST(TTabControl*,this)->SendMessage(TCM_GETTOOLTIPS));
  533. }
  534.  
  535. //
  536. // Assigns a tooltip control to the tab control. 
  537. //
  538. inline void TTabControl::SetToolTips(THandle toolTip)
  539. {
  540.   SendMessage(TCM_SETTOOLTIPS, TParam1(toolTip));
  541. }
  542.  
  543. //
  544. // Determines the index of the tab which is at the location 
  545. // specified in the 'pt' member of the 'htInfo' parameter.
  546. // Returns -1 if no tab is at the specified position.
  547. //
  548. inline int TTabControl::HitTest(TTabHitTestInfo& htInfo)
  549. {
  550.   return (int)SendMessage(TCM_HITTEST, 0,
  551.                             TParam2((TC_HITTESTINFO far*)&htInfo));
  552. }
  553.  
  554. #endif  // OWL_NATIVECTRL_ALWAYS
  555. #endif  // OWL_TABCTRL_H
  556.